package leetcode9;


/**
 * @Author tyy
 * @Description 9. 回文数
 * 给你一个整数 x ，如果 x 是一个回文整数，返回 true ；否则，返回 false 。
回文数是指正序（从左向右）和倒序（从右向左）读都是一样的整数。例如，121 是回文，而 123 不是。
链接：https://leetcode-cn.com/problems/palindrome-number
 * @Since 2021/10/5
 */
public class Solution {
    public int reverse3(int x) {
        int ans = 0;
        while (x != 0) {
            int pop = x % 10;
            if (ans > Integer.MAX_VALUE / 10 || (ans == Integer.MAX_VALUE / 10 && pop > 7))
                return 0;
            if (ans < Integer.MIN_VALUE / 10 || (ans == Integer.MIN_VALUE / 10 && pop < -8))
                return 0;
            ans = ans * 10 + pop;
            x /= 10;
        }
        return ans;
    }
    public int reverse2(int x) {
        int y = 0;
        while (x != 0) {
            if (y > 214748364 || y < -214748364) {
                return 0;
            }
            y = y * 10 + x % 10;
            x = x / 10;
        }
        return y;
    }
    public boolean isPalindrome(int x) {
        if (x < 0) {
            return false;
        }
        long y = 0L;
        long z = x;
        while (x != 0) {
            y = y * 10 + x % 10;
            x = x / 10;
        }
        return y == z;
    }
    public boolean isPalindrome2(int x) {
        if (x < 0) {
            return false;
        }
        String str = String.valueOf(x);
        String s = new StringBuilder(str).reverse().toString();
        return str.equals(s);
    }
    public boolean isPalindrome3(int x) {
        if(x < 0)
            return false;
        int cur = 0;
        int num = x;
        while(num != 0) {
            cur = cur * 10 + num % 10;
            num /= 10;
        }
        return cur == x;

    }
    public boolean reverse(int x) {
        String str = String.valueOf(x);
        String s = new StringBuilder(str).reverse().toString();
        String[] split = s.split("-", -1);
        if (split.length == 2) {
            String s1 = split[0];
            s = "-" + s1;
        }
        Long num = Long.parseLong(s);
        /*if (num > Integer.MAX_VALUE || num < Integer.MIN_VALUE) {
            return 0;
        }*/
        // int i = Math.toIntExact(num);
        return  (num == x);
    }
    public static void main(String[] args) {
        System.out.println("Integer.MAX_VALUE = " + Integer.MAX_VALUE);
        System.out.println("Integer.MIN_VALUE = " + Integer.MIN_VALUE);
        boolean reverse = new Solution().isPalindrome3(2147483647);
        System.out.println("reverse = " + reverse);
        int num = Integer.MAX_VALUE + 100;
        System.out.println("num = " + num);
        num = Integer.MAX_VALUE + 1;
        System.out.println("num = " + num);



    }
}
